﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Com.DA;
using Com.BU;
using System.Media;
using System.Linq;

namespace Com
{
    /// <summary>
    /// Interaction logic for ucColdStorageWrapping.xaml
    /// </summary>
    public partial class ucColdStorageWrapping : UserControl
    {
        public ucColdStorageWrapping()
        {
            this.InitializeComponent();
            loadData();
            enableButton();
        }
        public void loadData()
        {
            cbxDescription.Items.Clear();
            foreach (var item in LibraryManagerInCom.WrappingList)
            {
                cbxDescription.Items.Add(item);
            }
            cbxDescription.DisplayMemberPath = "wrapping_name";
            cbxDescription.SelectedIndex = 0;
        }

        private void cbxDescription_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (cbxDescription.SelectedItem != null)
            {
                txtSpecifictHeat.Text = ((tb_wrapping)cbxDescription.SelectedItem).specific_heat.ToString();
                txtWeight.Text = ((tb_wrapping)cbxDescription.SelectedItem).weight_rate.ToString();
            }

        }

        private void enableButton()
        {
            btnSave.IsEnabled = false;
            btnCancel.IsEnabled = false;

            btnNew.IsEnabled = true;
            btnDelete.IsEnabled = true;
            btnCopy.IsEnabled = true;
        }

        private void disableButtons()
        {
            btnSave.IsEnabled = true;
            btnCancel.IsEnabled = true;

            btnNew.IsEnabled = false;
            btnDelete.IsEnabled = false;
            btnCopy.IsEnabled = false;
        }

        private void btnNew_Click(object sender, RoutedEventArgs e)
        {
            cbxDescription.Text = "";
            txtSpecifictHeat.Text = "";
            txtWeight.Text = "";
            disableButtons();
        }

        private void btnCopy_Click(object sender, RoutedEventArgs e)
        {
            tb_wrapping wp = LibraryManagerInCom.copyColdStorageWrapping((tb_wrapping)cbxDescription.SelectedItem);
            cbxDescription.Text = wp.wrapping_name;
            txtSpecifictHeat.Text = wp.specific_heat.ToString();
            txtWeight.Text = wp.weight_rate.ToString();
            disableButtons();
        }

        private void btnDelete_Click(object sender, RoutedEventArgs e)
        {
            tb_wrapping wp = (tb_wrapping)cbxDescription.SelectedItem;
            LibraryManagerInCom.delWrapping(wp);
            loadData();
        }

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                tb_wrapping wp = new tb_wrapping();
                wp.wrapping_name = cbxDescription.Text;
                wp.specific_heat = Convert.ToDouble(txtSpecifictHeat.Text);
                wp.weight_rate = Convert.ToDouble(txtWeight.Text);

                LibraryManagerInCom.addWrapping(wp);
                enableButton();
                loadData();
            }
            catch (Exception)
            {

                throw;
            }

        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            loadData();
            enableButton();
        }

        #region Check null value
        private void CheckEmpty(object sender, TextChangedEventArgs e)
        {
            TextBox txt = (TextBox)sender;
            if (txt.Text == "")
            {
                txt.Text = "0";
            }
            if (txt.Text.Contains(' '))
            {
                txt.Text = txt.Text.Remove(txt.Text.IndexOf(' '), 1);
                SystemSounds.Beep.Play();
            }
            if (txt.Text.First() == '.')
            {
                txt.Text = txt.Text.Remove(0, 1);
                SystemSounds.Beep.Play();
            }
        }

        #endregion

        #region CHECK INPUT
        private void txtInput_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            if (Char.IsDigit((Char)e.Text[0]))
            {
                e.Handled = false;
            }
            else
            {
                if ((Char)e.Text[0] == '.')
                {
                    if (!((TextBox)sender).Text.Contains("."))
                    {
                        e.Handled = false;
                    }
                    else
                    {
                        e.Handled = true;
                        SystemSounds.Beep.Play();
                    }
                }
                else
                {
                    e.Handled = true;
                    SystemSounds.Beep.Play();
                }
            }
        }
        #endregion
    }
}